# -*- coding: utf-8 -*-
# @Time    : 2024/1/9 21:11
# @Author  : micah
# @File    : 11.jsonpath的简单使用.py
# @Software: PyCharm


import jsonpath

info = {
    "error_code": 0,
    "stu_info": [
        {
            "id": 2059,
            "name": "小白",
            "sex": "男",
            "age": 28,
            "addr": "河南省济源市北海大道xx号",
            "grade": "天蝎座",
            "phone": "1837830xxxx",
            "gold": 10896,
            "info": {
                "card": 12345678,
                "bank_name": '中国银行'
            }
        },
        {
            "id": 2067,
            "name": "小黑",
            "sex": "男",
            "age": 28,
            "addr": "河南省济源市北海大道xx号",
            "grade": "天蝎座",
            "phone": "87654321",
            "gold": 100
        }
    ]
}

"""不使用jsonpath提取数据"""
result_1 = info['stu_info'][0]['name']
result_2 = info['stu_info'][1]['name']
print(result_1, result_2)

print('-' * 30)

"""使用jsonpath提取数据"""
# jsonpath方法需要接收两个参数: 参数1[字典对象] 参数2[jsonpath语法]
result_1 = jsonpath.jsonpath(info, '$.stu_info[0].name')  # 返回的数据是一个列表类型
print(result_1)
result_2 = jsonpath.jsonpath(info, '$.stu_info[1].name')
print(result_2)

result_3 = jsonpath.jsonpath(info, '$..name')
print(result_3)

result_4 = jsonpath.jsonpath(info, '$..bank_name')
print(result_4)




